(() => {
    // 泛型，就是解决类、接口、方法的复用性，以及对不特定数据类型的支持

    // 1.泛型函数
    function getData<T, K>(str: T, num: K): [T, K] {
        let newArr: [T, K] = [str, num]
        return newArr
    }
    console.log(getData<string, number>('泛型', 1));


    // 2.泛型类
    class FindMin<T>{
        list: T[] = []
        add(value: T): void {
            this.list.push(value)
        }
        findMin(): T {
            let minVal = this.list[0]
            for (let i = 0; i < this.list.length; i++) {
                if (minVal > this.list[i]) {
                    minVal = this.list[i]
                }
            }
            return minVal
        }
    }
    // 找出string类型中的最小值
    const findStrMin = new FindMin<string>()
    findStrMin.add('9')
    findStrMin.add('4')
    findStrMin.add('6')
    console.log(findStrMin.findMin());

    // 找出number类型中的最小值
    const findNumMin = new FindMin<number>()
    findNumMin.add(10)
    findNumMin.add(6)
    findNumMin.add(2)
    console.log(findNumMin.findMin());


    // 3.泛型接口
    interface ConfigFn<T> {
        (valur: T): T
    }
    const getStrVal: ConfigFn<string> = <T>(str: T): T => {
        return str
    }
    const getNumVal: ConfigFn<number> = <T>(num: T): T => {
        return num
    }
    console.log(getStrVal('泛型接口'));
    console.log(getNumVal(1));


    // 4.把类作为参数类型的泛型类
    class User {
        name: string
        age: number
    }
    class Article {
        title: string
        desc: string
        status?: string
        constructor(params: {
            title: string
            desc: string
            status?: string
        }) {
            this.title = params.title
            this.desc = params.desc
            this.status = params.status
        }
    }
    class MysqlDb<T> {
        add(info: T): boolean {
            console.log(info);
            return true
        }
    }
    const user = new User()
    const article = new Article({ title: '力宏', desc: '出事了' })
    const mysqlDbUser = new MysqlDb<User>()
    const mysqlDbArticle = new MysqlDb<Article>()
    mysqlDbUser.add({ name: '张三', age: 18 })
    mysqlDbArticle.add(article)


})()